﻿#include "PropertyEditEntry.h"


#include <QApplication>
#include <QTimer>
QPointer<QTimer> propertyInspectorTimer() {
    static QPointer<QTimer> timer;
    if(!timer) {
        timer = new QTimer(QApplication::instance());
        timer->setInterval(1000/33);
        timer->start();
    }
    return timer;
}

PropertyEditEntry::~PropertyEditEntry() { }

PropertyEditEntry::PropertyEditEntry(QWidget *parent) : EditEntry{parent} {
    connect(propertyInspectorTimer().data(), &QTimer::timeout, this, &PropertyEditEntry::onTick);

    connect(this, &PropertyEditEntry::selectStateChanged, this, &PropertyEditEntry::refreshStylesheet);
    refreshStylesheet();
}

PropertyEditEntry::PropertyEditEntry(void *object, QWidget *parent) : EditEntry{parent} {
    setObject(object);
}

void PropertyEditEntry::refreshStylesheet() {
    QString background = "#A0A0A0";
    QString textColor = "#FFFFFF";
    if(isSelected()) {
        background = hasFocus() ? "#0078D7" : "#F0F0F0";
        if(hasFocus()) {
            textColor = "#000000";
        }
    }

    const auto qss = QString(R"(
        QWidget#%1 {
            background-color: %2;
            color: %3;
            font-weight: bold;
        }
    )") .arg(editPanel()->objectName()).arg(background).arg(textColor);
    editPanel()->setStyleSheet(qss);
}

